from selenium import webdriver


def update_cookies():
    driver = webdriver.Chrome(r'F:\Chrome\chrome插件\chromedriver_win32-81\chromedriver.exe')
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => undefined
        })
      """
    })
    driver.get('http://www.nhc.gov.cn/xcs/yqtb/list_gzbd.shtml')
    cookies_dic = driver.get_cookies()
    # print(cookies_dic)

    items = []
    cookies = ''
    for i in range(len(cookies_dic)):
        cookie_value = cookies_dic[i]
        item = cookie_value['name'] + '=' + cookie_value['value']
        items.append(item)
        # print(items)
        cookies = ';'.join(x for x in items)
    # print(cookies)
    driver.close()
    print('cookies已更新')
    return cookies
